package com.base.commons.log.jaeger;

import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import com.base.commons.tools.utils.SpringContextUtil;
import io.opentracing.Tracer;
import io.opentracing.contrib.spring.cloud.log.SpanLogsAppender;
import org.slf4j.LoggerFactory;


public class JaegerLogAppender {

    public static void start() {
        Logger rootLogger = getRootLogger();
        if(rootLogger.getAppender("SpanLogsAppender")==null) {
            Tracer tracer = SpringContextUtil.getBean(Tracer.class);
            SpanLogsAppender spanLogsAppender = new SpanLogsAppender(tracer);
            spanLogsAppender.start();
            rootLogger.addAppender(spanLogsAppender);
        }
    }

    private static Logger getRootLogger() {
        LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
        return context.getLogger("ROOT");
    }
}